<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <script>
        function ajaxFunc(method, url, data, callback, flag, file) {//传输方式英文大写，地址，后台接受数据，回调函数，是否异步，是否有文件上传
            var xhr = new XMLHttpRequest() || new ActiveXObject('Microsoft.XMLHttp');//后者兼容ie
            method = method.toUpperCase();
            if (method == 'GET') {
                xhr.open(method, url + '?' + data, flag);
                xhr.send();
            } else if (method == 'POST') {
                xhr.open(method, url, flag);
                xhr.setRequestHeader('Content-type', file ? 'multipart/form-data' : 'application/x-www-form-urlencoded');//请求头只有post才需要写后面是编码方式 传文件需要更改
                xhr.send(data);//post传输数据是写到send里面
            }
            xhr.onreadystatechange = function () {
                // console.log(xhr.readystate);
                if (xhr.readyState == 4) { //已经成功返回数据
                    if (xhr.status == 200) {
                        callback(xhr.responseText);
                    }
                }
            }
        }
    </script>
</body>

</html>